/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webidewar.system;

/**
 *
 * @author huy
 */
public class PythonCodeModifier extends CodeModifier {

    @Override
    public String convert(String _in) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String re = _in;
        //re = CodeModifier.stripHTML(re);
        re = createObject(_in, "ProcessesManager", "pm");
        re = importPackages(re, "from webidewar.system import ProcessesManager");
        re = replaceIOFunctions(re);
        return re;
    }

    private String importPackages(String _in, String... str) {
        String re = "";
        for (int i = 0; i < str.length; i++) {
            re = re + str[i] + "\n";
        }
        re += _in;
        return re;
    }
    
    private String createObject(String _in,String _type, String _name){
        String re = "";
        re+=_name+"="+_type+"()\n";
        re+=_in;
        return re;
        
    }

    private String replaceIOFunctions(String _in) {
        String re = _in;
        re = replaceFunction(re, "print", "pm.print");
        re = replaceFunction(re, "input", "pm.input");
        return re;
    }

    private String replaceFunction(String _in, String _old, String _new) {
        String re = _in;
        re = re.replace(_old + "(", _new + "(" + getId() + ",");
        return re;
    }
}
